

$(function(){

	var DataAll=[];
	var data=[];//所有图形数据
	var url="getPrisonAllData.do";
	diTime();
	
	var dataQuery={};
	requestData(1,url,null);
	var infoc=[];
	var month= ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"];

	$('#myTabDrop1').click(function(){
		$('.mmmm').toggle();
		
	})
	//柱状时间查询按钮被点击
	$("#search").click(function(){
		$("#cpo1").html("");
		var url="getPrisonAllData.do";
		requestTime(1,url);
	})
	//扇形时间查询按钮被点击
	$("#search1").click(function(){
		$("#cpo3").html("");
		var url="getPrisonAllData.do";
		requestTime(3,url);
	})
	/*条形统计图被单击*/
	$("#getStrip").click(function(){
		$('.mmmm').hide();

		$("#cpo1").html("");
		var url="getPrisonAllData.do";
		requestData(1,url,null);
	})
	
	/*扇形统计图被单击*/
	$("#getFanned").click(function(){
		$('.mmmm').hide();
		$("#cpo3").html("");
		
		var url="getPrisonAllData.do";
		requestData(3,url,null);
	})
//时间查询
function  requestTime(flag,url)
{
	var start=0;
	var end=0;
	if(flag==3){
		start=$("#valStart1").val();
		end=$("#valEnd1").val();
	}else{
		start=$("#valStart").val();
		end=$("#valEnd").val();
	}
	var array=[];
	array=YuXi.timestamp.get(start,end,null);
	dataQuery.starTime=array[0];
	dataQuery.endTime=array[1];
	requestData(flag,url,dataQuery);
}	
function diTime(){
	var time=new Date();
	var ti=time.getFullYear();
	var tm=time.getMonth()+1;
	
	for(var i=ti;i>=2014;i--)
	{
		var html="<option value='"+i+"'>"+i+"</option>" ;
		$(".diTime").find("select").append(html);

	}
	for(var i=tm;i>=1;i--)
	{
		var html="<option value='"+i+"'>"+i+"</option>" ;
		$(".drTime").find("select").append(html);

	}
	
}
//获取饼图和柱形图数据
function requestData(index,url,dataQuery)
{
	var url=url;//获取所有数据-------
	YuXi.util.get(url,dataQuery,function(response){
		data=response.object;
		if(index==1){
			requestAll(1);
		}
		else if(index==2)
		{
			requestAll(2);
		}
		else
		{
			requestAll(3);
		}
	},function(){
	})
}


function requestAll(flag)
{	
	var dataSouce=[];
	DataAll=[];
	color=["#08BDFF","#003C59","#5B93B0","#ED0000","#ff8c31","#ACABAB","#323032","#FF6300","#FF6300","#FF9418"];
	if(flag==1 || flag==3)
	{
		dataSouce=data;
		for(var i=0;i<dataSouce.length;i++)
		{	
			var curdata=isHaved(dataSouce[i].prisonId);
			if(curdata!=null){
			
				DataAll[curdata].datasets.push({
							flagColor:color[i],
							safetyName:dataSouce[i].safetyPortNames,
							infocount:dataSouce[i].infoCount,
							barItemName: dataSouce[i].safetyPortNames,
							fillColor : color[i],
							strokeColor : "rgba(220,220,220,1)",
							data : [dataSouce[i].infoManage,dataSouce[i].firstCheck,dataSouce[i].reCheck,dataSouce[i].infoCheck,dataSouce[i].infoReSeach,dataSouce[i].taskDis,dataSouce[i].curTaskDis,dataSouce[i].taskFinsh]});//dataSouce[i].infoComment,dataSouce[i].infoRate,dataSouce[i].infoFinsh]					});
			}else{
			
				DataAll.push({
					id:dataSouce[i].prisonId,
					name:dataSouce[i].name,
					labels : ["信息管理","初审","复核","审核","信息研判","任务派遣","即时任务派遣","任务完成情况"],//,"点评","评价","已完成信息"],
					datasets : [
						{
							flagColor:color[i],
							safetyName:dataSouce[i].safetyPortNames,
							infocount:dataSouce[i].infoCount,
							barItemName: dataSouce[i].safetyPortNames,
							fillColor : color[i],
							strokeColor : "rgba(220,220,220,1)",
							data : [dataSouce[i].infoManage,dataSouce[i].firstCheck,dataSouce[i].reCheck,dataSouce[i].infoCheck,dataSouce[i].infoReSeach,dataSouce[i].taskDis,dataSouce[i].curTaskDis,dataSouce[i].taskFinsh]//dataSouce[i].infoComment,dataSouce[i].infoRate,dataSouce[i].infoFinsh]
						}					
					]
				});
				
			}
		}	


	}
	else
	{
		
		DataAll=[];
		dataSouce=data;

		for(var i=0;i<dataSouce.length;i++)
		{	
		
			var curdata=isHaved(dataSouce[i].prisonId);
			if(dataSouce[i].monthName==undefined)
			{
		
				var mo="第"+parseInt(dataSouce[i].weekName)+"周";
			}
			else
			{
				var mon=parseInt(dataSouce[i].monthName);
				mo=month[mon-1];
			}
			
		
			if(curdata!=null){
				DataAll[curdata].dataset.push({
						label:mo,
						
						count:dataSouce[i].infoCount
						
					
				})
			}
			else
			{
				DataAll.push({
				labels:[],
				id:dataSouce[i].prisonId,
				dataset: [
					{
						label:mo,
						
						count:dataSouce[i].infoCount,
					
					}],
				datasets : [
					{
						lineItemName : dataSouce[i].name,
						fillColor : "rgba(220,220,220,0.5)",
						strokeColor : color[i],
						pointColor : color[i],
						pointStrokeColor : "#fff",
						count:dataSouce[i].infoCount,
						data : []
					}]
				})

			}
			
		}
		   
	}
		//return false;
		for(var i=0;i<DataAll.length;i++)
		{
			col(DataAll[i],flag);
			
		}

		if(flag==3)
		{
			requestFanned();
		}
}
function col(data,flag){

	if(flag==1)
	{
		var max=0;
		var nex=0;
		for(var i=0;i<data.datasets.length;i++)
		{
			
			nex=Math.max.apply(null,data.datasets[i].data);
			
			data.datasets[i].flagColor=color[i];

			data.datasets[i].fillColor=color[i];
			if(max<nex)
			{
				max=nex;
			}
		}
		
		max=max+3;
	
		requestStrip(data,max);
	}
	else if(flag==2)
	{
		for(var i=0;i<data.datasets.length;i++)
		{
			data.datasets[i].strokeColor=color[i];
			data.datasets[i].flagColor=color[i];
			data.datasets[i].pointColor=color[i];
		}

		Line(data);
		
	}
	else
	{
		for(var i=0;i<data.datasets.length;i++)
		{
			data.datasets[i].flagColor=color[i];
			data.datasets[i].fillColor=color[i];
		}
		infco(data);
			
	}
}
function isHaved(prisonId){		
		for(var i=0;i<DataAll.length;i++){
			if(DataAll[i].id==prisonId){

				return i;
			}
		}
		
		return null;
	}
function Line(data)
{
	
	data.datasets.data=[];
	for(var i=0;i<data.dataset.length;i++)
	{
		data.datasets[0].data.push(data.dataset[i].count);
		data.labels.push(
			data.dataset[i].label
		)
	}
	
	requestLine(data);
}
//获取条形图
function requestStrip(data,max)
{			
	var html='<div class="sort" >';	
	for(var i=0;i<data.datasets.length;i++)
	{
		html=html+'<span class="flagcolor" style="background-color:'+data.datasets[i].flagColor+'"></span>'+data.datasets[i].safetyName;
	}
	html=html+'</div>';
	var header="<h3>"+data.name+"各分口流程信息数量</h3>";
	
			setTimeout(function(){
				init();

			},20)
			function init(){
				var curcan=document.createElement("canvas");
				curcan.width=1000;
				curcan.height=400;
				$("#cpo1").append(header);
				$("#cpo1").append(html);
				$("#cpo1").append(curcan);
				var ver=checkVersion();
				
				if(ver)
				{
					curcan=window.G_vmlCanvasManager.initElement(curcan);
				}

				var ctx = curcan.getContext("2d");
				var option={
					scaleOverride :true ,   //是否用硬编码重写y轴网格线
				 scaleSteps :max,        //y轴刻度的个数
				scaleStepWidth : 1,   //y轴每个刻度的宽度
				scaleStartValue : 0   //y轴的起始值



				}
				var chartBar = new Chart(ctx).Bar(data,option);	
				/*,{
                scaleOverride :true ,   //是否用硬编码重写y轴网格线
                scaleSteps : 12,        //y轴刻度的个数
                scaleStepWidth : 1,   //y轴每个刻度的宽度
               
                pointDot : true,        //是否显示点
                pointDotRadius : 5,     //点的半径
                pointDotStrokeWidth : 1,//点的线宽
                datasetStrokeWidth : 3, //数据线的线宽
               
               } 				*/
				initEvent(chartBar, clickCall);			
				
			}
			
			function clickCall(evt){
				var activeBar = chartBar.getBarSingleAtEvent(evt);
				//if ( activeBar !== null )
					//alert(activeBar.label + ": " + activeBar.barItemName + " ____ " + activeBar.value);
			}
			
			function initEvent(chart, handler) {
				var method = handler;
				var eventType = "click";
				var node = chart.chart.canvas;
								
				if (node.addEventListener) {
					node.addEventListener(eventType, method);
				} else if (node.attachEvent) {
					node.attachEvent("on" + eventType, method);
				} else {
					node["on" + eventType] = method;
				}
			}
}
//获取线性图
function requestLine(data)
{
	var header="<h3>"+data.datasets[0].lineItemName+"信息数量折线图</h3>";
	setTimeout(function(){
				init();

			},20)
	function init()
	{
		var curcan=document.createElement("canvas");
		curcan.width=1000;
		curcan.height=400;
		if(data.labels[0].indexOf("第"))
		{
			
			$("#cpo2").append(header);
			$("#cpo2").append(curcan);
		}
		else
		{
			$("#week1").append(header);
			$("#week1").append(curcan);
		}
		
		
		var ctx = curcan.getContext("2d");
		chartLine = new Chart(ctx).Line(data);
		initEvent(chartLine, clickCall);

	}

			
			function clickCall(evt) {
				var point = chartLine.getPointSingleAtEvent(evt);
				
				//if ( point !== null )
				//	alert( point.label + ": " + point.lineItemName + " ____ " + point.value);
			}
			
			function initEvent(chart, handler) {
				var method = handler;
				var eventType = "click";
				var node = chart.chart.canvas;								
				if (node.addEventListener) {
					node.addEventListener(eventType, method);
				} else if (node.attachEvent) {
					node.attachEvent("on" + eventType, method);
				} else {
					node["on" + eventType] = method;
				}
			}
}
//扇形
function infco(data){

	var x=0;
	for(var i=0;i<data.datasets.length;i++)
	{
		x=x+data.datasets[i].infocount;
	}
	infoc.push({
		number:x,
		name:data.name
		 ,text: data.name+":"+x
		
	});

}
//获取扇形图
function requestFanned()
{
	var html='<div class="sort" >';	
	var value=[];
	value.push({
			value:0,
			name:"",
			color:color[0],
			text:""
		})
	for(var i=0;i<infoc.length;i++)
	{
		html=html+'<span class="flagcolor" style="background-color:'+color[i]+'"></span>'+infoc[i].name;
		value.push({
			value:infoc[i].number,
			name:infoc[i].name,
			color:color[i],
			text:infoc[i].text
		})
		
	}
	
		
	html=html+'</div>';
	setTimeout(
		function(){
				init();

			},20)
	
	function init()
	{
		var cur=document.createElement("canvas");
		cur.id="canvas";
		cur.width=1000;
		cur.height=400;
		$("#cpo3").append(html);
		$("#cpo3").append(cur);
		var ver=checkVersion();
				
		if(ver)
		{
			cur=window.G_vmlCanvasManager.initElement(cur);
		}
				
		
		/*var ctx = curcan.getContext("2d");
		new Chart(ctx).Doughnut(value);*/
		var ctx=new Chart(cur.getContext("2d")).Pie(value);
		
	}
	infoc=[];




}


   function getInternetExplorerVersion()
 // Returns the version of Windows Internet Explorer or a -1
 // (indicating the use of another browser).
 {

    var rv = -1; // Return value assumes failure.
   if (navigator.appName == 'Microsoft Internet Explorer')
   {
       var ua = navigator.userAgent;
       var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
       if (re.exec(ua) != null)
          rv = parseFloat( RegExp.$1 );
    }
    return rv;
 }
 function checkVersion()
 {
  var name="";
    var msg = "You're not using Windows Internet Explorer.";
   var ver = getInternetExplorerVersion();
    if ( ver> -1 )
    {
       if ( ver>8.0 )
       {
          return false;

       } 
      else
         return true;
     }
    else
     {

         return false;
     }
   
    }


});



	